/**
* @module State
*/
function State(){
	this.init();
};

State.prototype.init = function() {

  this.code = {
    init: [],
    remote: [],
    local: []
  };
  this.remote = false,
  this.loop = {};
  this.remoteVars = [];

  this.loopdepth = 0;
  this.outputVarNr = 0;
  //this.conddepth = 0;
  //this.calldepth = 0;
};

State.prototype.addRemoteVar = function(name) {
  this.remoteVars.push(name);
}

State.prototype.removeRemoteVar = function(name) {
  var remoteVars = [];
  for( var i = 0; i < this.remoteVars.length; i++ ) {
    if( this.remoteVars[i] != name ) {
      remoteVars.push(remoteVars[i])
    }
  }
  this.remoteVars = remoteVars;
}

State.prototype.isRemote = function(name) {
  var remoteVars = this.getRemoteVars();

  for( var i = 0; i < remoteVars.length; i++ ) {
    if( remoteVars[i] == name ) {
      return true;
    }
  }

  return false;
}

State.prototype.traverseIsRemote = function(node) {
  // Check if left hand side is remote
  var state = this;
  var remote = false;

  node.traverseTopDown('Var(x)', function(b) {
    if( state.isRemote(b.x.value) ) {
      // Remote
      remote = true;
      return;
    }
  });

  return remote;
}

State.prototype.getRemoteVars = function() {
  return this.getRemoteLoopVars(this.remoteVars);
}

State.prototype.getRemoteLoopVars = function(mergeArray) {
  var vars;
  if( mergeArray ) {
    vars = mergeArray;
  }
  else {
    vars = [];
  }
  var depth = this.loopdepth;

  while( depth > 0 ) {
    if( this.loop[depth].mode == 'both' ) {
      vars.push(this.loop[depth].item);
      depth = depth - 1;
    }
  }

  return vars;
}

exports.State = State;